package com.jingmi.jingmiliving.commodity.service.impl;

import com.jingmi.jingmiliving.commodity.dao.BrandDao;
import com.jingmi.jingmiliving.commodity.dao.CategoryDao;
import com.jingmi.jingmiliving.commodity.entity.BrandEntity;
import com.jingmi.jingmiliving.commodity.entity.CategoryEntity;
import com.jingmi.jingmiliving.commodity.service.BrandService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingmi.common.utils.PageUtils;
import com.jingmi.common.utils.Query;

import com.jingmi.jingmiliving.commodity.dao.CategoryBrandRelationDao;
import com.jingmi.jingmiliving.commodity.entity.CategoryBrandRelationEntity;
import com.jingmi.jingmiliving.commodity.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Resource
    BrandDao brandDao;
    @Resource
    CategoryDao categoryDao;
    @Resource
    private BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAll(CategoryBrandRelationEntity categoryBrandRelation) {

         categoryBrandRelation.setBrandName(brandDao.selectById(categoryBrandRelation.getBrandId()).getName());
         categoryBrandRelation.setCategoryName(categoryDao.selectById(categoryBrandRelation.getCategoryId()).getName());
         this.save(categoryBrandRelation);


    }

    @Override
    public List<CategoryBrandRelationEntity> queryByBrandId(Long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelation =
                this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

        return categoryBrandRelation;
    }

    @Override
    public List<BrandEntity> queryByCategoryId(Long categoryId) {
        List<CategoryBrandRelationEntity> categoryId1 =
                this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("category_id", categoryId));
        List<BrandEntity> collect = categoryId1.stream().map(obj -> {
            Long brandId = obj.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());

        return collect;
    }

}